﻿using System;
using UnityEngine;

namespace Framework
{
    public class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
    {
        public static T instance;

        public virtual void Awake()
        {
            if (instance == null)
            {
                instance = (T)this;
            }
            else
            {
                throw new InvalidOperationException("This is a singleton, type is " + typeof(T) + ".");
            }
        }
        
        public virtual void OnDestroy()
        {
            instance = null;
        }
    }
}